package utils;

import android.content.Context;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import view.MyAddressPicker;

/**
 * Created by Administrator on 2017/3/29.
 */

public class AddressDbUtils {
    public static ArrayList<MyAddressPicker.Province> getProvince(Context context) {
        ArrayList<MyAddressPicker.Province> address = null;
        try {
            InputStream open = context.getAssets().open("address.xml");
            SAXForHandler saxForHandler = new SAXForHandler();
            /**
             * 用工廠模式解析XML
             */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser saxParser = spf.newSAXParser();
            saxParser.parse(open, saxForHandler);
            address = saxForHandler.getAddress();
            open.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return address;

    }

    private static class SAXForHandler extends DefaultHandler {
        //解析器
        private ArrayList<MyAddressPicker.Province> address;
        private ArrayList<MyAddressPicker.City> cities;
        private ArrayList<MyAddressPicker.County> counties;
        private StringBuilder builder;
        private MyAddressPicker.Province province;
        private MyAddressPicker.City city;
        private MyAddressPicker.County county;

        //返回解析后得到的Book对象集合
        public ArrayList<MyAddressPicker.Province> getAddress() {
            return address;
        }

        @Override
        public void startDocument() throws SAXException {
            super.startDocument();
            address = new ArrayList<>();
            cities = new ArrayList<>();
            counties = new ArrayList<>();
            builder = new StringBuilder();
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            super.startElement(uri, localName, qName, attributes);
            switch (localName) {
                case "provname":
                    cities = new ArrayList<>();
                    province = new MyAddressPicker.Province();
                    break;
                case "cityname":
                    counties = new ArrayList<>();
                    city = new MyAddressPicker.City();
                    break;
                case "distname":
                    county = new MyAddressPicker.County();
                    break;
                default:
                    break;
            }
            builder.setLength(0);   //将字符长度设置为0 以便重新开始读取元素内的字符节点
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            super.characters(ch, start, length);
            builder.append(ch, start, length);  //将读取的字符数组追加到builder中
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            super.endElement(uri, localName, qName);
            if (localName.equals("provname")) {
                province.setAreaName(builder.toString());
                province.setCities(cities);
                address.add(province);
                province = null;
            } else if (localName.equals("cityname")) {
                city.setAreaName(builder.toString());
                city.setCounties(counties);
                cities.add(city);
                city = null;
            } else if (localName.equals("distname")) {
                county.setAreaName(builder.toString());
                counties.add(county);
                county = null;
            }
        }
    }

}
